<template>

  <el-form ref="ruleForm" :model="ruleForm" status-icon label-width="100px" :rules="rules" class="demo-ruleForm" @close="close">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="ruleForm.name" autocomplete="off" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="ruleForm.code" autocomplete="off" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="ruleForm.manager" placeholder="请选择" @focus="allManager">
        <el-option
          v-for="item in staffSimpleList"
          :key="item.id"
          :label="item.username"
          :value="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="ruleForm.introduce" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit('ruleForm')">提交</el-button>
      <el-button @click="close">取消</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
import { reqAddDepartment, reqEditDepartment, reqQueryDepartmentDetail, reqStaffSimple } from '@/api/departments'
export default {
  name: 'AddDepartment',
  props: {
    nodeData: {
      type: Object,
      required: true

    },
    departsList: {
      type: Array,
      required: true

    }
  },
  data() {
    const validateName = (rules, value, callback) => {
      let resultBoolean = ''
      if (this.ruleForm.id) {
        // 需求：如果是编辑模式，可以和自己名字一样 && 不可以与自己的兄弟名字一样
        if (value === this.nodeData.name) return callback()
        resultBoolean = this.departsList.filter(item =>
          item.pid === this.nodeData.pid
        ).some(item => item.name === value)
      } else {
        // 根据nodedata的id找到所有的子部门查看name是否有相等的
        resultBoolean = this.departsList.filter(item =>
          item.pid === this.nodeData.id
        ).some(item => item.name === value)
      }
      resultBoolean ? callback(new Error('部门名称已经重复了哦')) : callback()
    }
    const validateCode = (rules, value, callback) => {
      // 需求：如果是编辑模式code是可以相等的,如果不相等还要校验是否与其他的部门的code是否相等
      if (this.ruleForm.id && value === this.nodeData.code) {
        callback()
        return
      }
      // 根据nodedata的id找到所有的部门查看code是否有相等的
      const resultBoolean = this.departsList.some(item => item.code === value)
      resultBoolean ? callback(new Error('编码已经重复了哦')) : callback()
    }
    return {
      ruleForm: {
        code: '',
        introduce: '',
        manager: '',
        name: ''

      },
      staffSimpleList: [],
      dialogFormVisible: true,
      rules: {
        code: [
          { required: true, message: '此项为必填选项' },
          { min: 1, max: 10, message: '要求为1~10的字符' },
          { validator: validateCode, trigger: 'blur' }

        ],
        introduce: [
          { required: true, message: '此项为必填选项' },
          { min: 1, max: 10, message: '要求为1~10的字符' }
        ],
        manager: [
          { required: true, message: '此项为必填选项' }
        ],
        name: [
          { required: true, message: '此项为必填选项' },
          { validator: validateName, trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    async queryDepartmentDetail() {
      const data = await reqQueryDepartmentDetail(this.nodeData.id)
      // console.log(data)
      this.ruleForm = data
    },

    close() {
      this.$emit('closeDialog')
      console.log('close')
      this.reset()
    },
    reset() {
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
    },
    async allManager() {
      const res = await reqStaffSimple()
      this.staffSimpleList = res
    },
    async submit(ref) {
      try {
        await this.$refs[ref].validate()
      } catch {
        return this.$message.error('校验没通过')
      }
      // 如果有form。id证明是编辑，否则是添加
      if (this.ruleForm.id) {
        await reqEditDepartment(this.ruleForm)
      } else {
        await reqAddDepartment({
          ...this.ruleForm,
          pid: this.nodeData.id
        })
      }
      this.close()
      this.$emit('getDepartmentsList')
    }
  }

}
</script>

<style>

</style>
